package com.qingfeng.coupon.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingfeng.client.api.BaseV1Api;
import com.qingfeng.client.bean.Buyer;
import com.qingfeng.client.util.Page;
import com.qingfeng.coupon.Do.CouponDetail;
import com.qingfeng.coupon.Vo.CouponQuery;
import com.qingfeng.coupon.constant.CouponExpireType;
import com.qingfeng.coupon.dao.CouponMapper;
import com.qingfeng.coupon.dao.CouponTemplateMapper;
import com.qingfeng.coupon.dao.CouponUseGoodsMapper;
import com.qingfeng.coupon.entity.CouponTemplate;
import com.qingfeng.coupon.entity.CouponUseGoods;
import com.qingfeng.coupon.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/5/28 10:06 上午
 */

@Api(tags = "优惠券")
@RestController
@RequestMapping("/coupon")
public class CouponApi extends BaseV1Api {


    @Autowired
    private CouponService couponService;

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponTemplateMapper templateMapper;

    @Autowired
    private CouponUseGoodsMapper useGoodsMapper;


    @ApiOperation("获取当前商品可用优惠券")
    @GetMapping("/getUsableCoupon/{goodsCommonId}")
    @ApiImplicitParam(name = "goodsCommonId",value = "商品通用属性id",dataType = "Long",required = true)
    public Object getUsableCoupon(@PathVariable Long goodsCommonId){
        Buyer buyer = getUser();
        List<CouponTemplate> list = couponService.getDistributeTemplates(goodsCommonId,buyer);
        return success(list);
    }

    @ApiOperation("查看个人所有优惠券")
    @PostMapping("/myCoupon")
    public Object myCoupon(@RequestBody CouponQuery query){
        Buyer buyer = getUser();
        IPage<CouponDetail> page1 = couponMapper.selectByUserIdAndStatus(query.getPage(),
                buyer.getId(),
                query.getCondition());
        page1.getRecords().forEach(item->{
            if (item.getExpirePeriod().equals(CouponExpireType.FIX_DATA.getCode())) {
                item.setStart(item.getAssignTime());
                long endtime = item.getAssignTime().getTime()
                        + (item.getExpireGap() * 24 * 3600000L);
                item.setEnd(new Date(endtime));
            } else {
                item.setStart(item.getExpireStart());
                item.setEnd(item.getExpireEnd());
            }
        });

        return successPage(page1);
    }


    @ApiOperation("领取优惠券")
    @GetMapping("/receiveCoupon/{couponTemplateId}")
    @ApiImplicitParam(name = "couponTemplateId",value = "优惠券模版id",dataType = "Long",required = true)
    public Object receiveCoupon(@PathVariable Long couponTemplateId){
        Buyer buyer = getUser();
        couponService.receiveCoupon(couponTemplateId,buyer.getId());
        return success("领取成功");
    }

    @ApiOperation("查看优惠券详情")
    @GetMapping("/detail/{couponId}")
    @ApiImplicitParam(name = "couponId",value = "优惠券id",dataType = "Long",required = true)
    public Object getDeatil(@PathVariable Long couponId){
        Buyer buyer = getUser();
        CouponDetail couponDetail = couponMapper.selectDetailById(couponId,buyer.getId());
        if (couponDetail != null){
            if (couponDetail.getExpirePeriod().equals(CouponExpireType.FIX_DATA.getCode())) {
                couponDetail.setStart(couponDetail.getAssignTime());
                long endtime = couponDetail.getAssignTime().getTime()
                        + (couponDetail.getExpireGap() * 24 * 3600000L);
                couponDetail.setEnd(new Date(endtime));
            } else {
                couponDetail.setStart(couponDetail.getExpireStart());
                couponDetail.setEnd(couponDetail.getExpireEnd());
            }
            List<CouponUseGoods> goods = useGoodsMapper.selectByTemplateId(couponDetail.getTemplateId());
            couponDetail.setUseGoods(goods);
            return success(couponDetail);
        }
        return error("该优惠券不存在");
    }



}
